package com.transcendence.appwidget.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import com.transcendence.appwidget.R;
import com.transcendence.appwidget.twofive.MyService;
import com.transcendence.appwidget.twofive.TwoFiveAppWidget;
import com.transcendence.appwidget.twofour.TwoFourAppWidget;
import com.transcendence.core.global.Global;
import com.transcendence.core.utils.log.LogUtils;
import com.transcendence.core.utils.sp.SPUtils;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        startService(new Intent(this, UpdateTimeService.class));
//        boolean flag = SPUtils.getInstance().get("goto_edit",false);
//        LogUtils.d("MainActivity onCreate:"+flag);
//        if(flag){
//            Intent intent = new Intent(this,EditActivity.class);
//            startActivity(intent);
//            finish();
//        }
        findViewById(R.id.tv_notice).setOnClickListener(v->{
            LogUtils.d("发广播");
//            Intent updateIntent = new Intent();
//
//            updateIntent.setAction(Global.REFRESH_LOGIN);
//            sendBroadcast(updateIntent);
//            Intent service = new Intent(this, MyService.class);
//            startService(service);

            // 登录成功后，更新小部件UI
            updateWidgetUI();
        });
    }

    private void updateWidgetUI() {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, TwoFiveAppWidget.class));
        Intent intent = new Intent(this, TwoFiveAppWidget.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.setAction(Global.REFRESH_LOGIN);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        sendBroadcast(intent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        LogUtils.d("MainActivity onNewIntent");
    }


//    private void registerBoradcastReceiver() {
//        IntentFilter myIntentFilter = new IntentFilter();
//        myIntentFilter.addAction(Global.REFRESH_LOGIN);
//        // 注册广播
//        registerReceiver(mBroadcastReceiver, myIntentFilter);
//    }
}